package com.bosssoft.cloud.wechat.core.infra.config;

import com.bosssoft.cloud.wechat.core.infra.properties.WxTempletProperties;
import com.bosssoft.cloud.wechat.core.infra.properties.WxTempletProperties.TextTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 微信模板类
 * <p>
 * 微信公众号包括模板消息，普通文本消息，事件，菜单等所有模板在此处
 *
 * @author wcy create on 2021/7/8 11:31 上午
 */
@Component
public class WxTemplet {

    private static final String TEXT_DEFAULT_MSG = "default_msg";

    @Resource
    private WxTempletProperties wxTemplateProperties;

    public static WxTempletProperties.SendTemplate send;
    public static WxTempletProperties.TextTemplate text;
    public static WxTempletProperties.EventTemplate event;
    public static WxTempletProperties.MenuTemplate menu;


    @PostConstruct
    public void init() {
        send = wxTemplateProperties.getSend();
        text = wxTemplateProperties.getText();
        event = wxTemplateProperties.getEvent();
        menu = wxTemplateProperties.getMenu();
    }

    /**
     * 通过模板消息的id，获取模板消息
     *
     * @param sendId id
     * @return 模板消息
     */
    public static WxTempletProperties.SendTemplate.SendData getSendBySendId(String sendId) {
        for (WxTempletProperties.SendTemplate.SendData send : send.getData()) {
            if (send.getSendId().equals(sendId)) return send;
        }
        return null;
    }

    public static TextTemplate.TextData getTextData(String content) {
        return getTextData(content, text.getMustReply());
    }


    public static TextTemplate.TextData getTextData(String content, Boolean mustReply) {
        TextTemplate.TextData data = text.getData().get(content);
        return data == null && mustReply ? text.getData().get(TEXT_DEFAULT_MSG) : data;
    }
}
